package com.ice.base.controller;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.ice.base.constant.BaseConstant;
import com.ice.base.util.SmsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@ApiIgnore(value = "发送短信接口")
public class SmsSendController extends  BaseController{

    @Autowired
    private StringRedisTemplate template;
 /*   @Autowired
    private UserService userService;*/


    @ApiOperation(value = "发送短信")
    @PostMapping("/api_p/sms/getCode")
    @ApiImplicitParam(name = "msgType",value = "参数msgType 1 注册;2 忘记密码;3 验证码登录")
    public HashMap getNoteCode(@RequestParam(name = "phone") String phone, @RequestParam(name = "msgType") String msgType) {

//        UserModel model  = userService.selectByUserName(phone); //校验用户是否存在
        //  msgType 1 注册短信; 2 忘记密码 ; 3 验证码登录
        HashMap<String, Object> returnMap = getReturnMap();
        String signName = "";
        String msgMode = "";
      /*  if(msgType.equals("1")){
            if(model != null){
                return getReturnMap(BaseConstant.Response_MENU.REQUEST_USER_EXISTS);
            }
            signName = "身份验证";
            msgMode = "SMS_68265060";
        }else if(msgType.equals("2")){
            if(model == null){
                return getReturnMap(BaseConstant.Response_MENU.REQUEST_USER_NOTFOUND);
            }
            signName = "身份验证";
            msgMode = "SMS_68265058";
        }else if(msgType.equals("3")){
            if(model == null){
                return getReturnMap(BaseConstant.Response_MENU.REQUEST_USER_NOTFOUND);
            }
            signName = "登录验证";
            msgMode = "SMS_68265062";
        }
*/
        try {
            HashMap<String, Object> resultMap = getReturnMap();
            HashMap hashMap = SmsUtils.sendMsg(phone, signName, msgMode);

            SendSmsResponse sendSmsResponse = (SendSmsResponse)hashMap.get("sendSmsResponse");
            //验证码
            String checkCode = (String) hashMap.get("checkCode");
            String status =  (String) hashMap.get("status");
            String message = sendSmsResponse.getMessage();
            String code = (String)sendSmsResponse.getCode();
            if ("0".equals(status)) {
                getReturnMap(BaseConstant.Response_MENU.REQUEST_USER_CODE_FAILED,returnMap);
                if("isv.BUSINESS_LIMIT_CONTROL".equals(code)){
                    returnMap.put("message","获取验证码太频繁,请稍后再试！");
                }
                return returnMap;
            }

            //植入到redis中
            template.opsForValue().set(phone + msgType, checkCode, 5, TimeUnit.MINUTES);
            resultMap.put("data", "短信验证码已发送到" + phone );
            resultMap.put("code", code);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            return getReturnMap(BaseConstant.Response_MENU.REQUEST_SYSTEM_FAILED);
        }
    }
}
